package business.impl;

import business.BusinessFacade;
import dao.GenericDao;
import domain.impl.AbstractBusinessObject;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import vo.ValueObject;

import java.util.List;

/**
 * Created by NL
 * Sur Soluciones TI
 * Date: 30/03/11
 */

public abstract class BusinessFacadeImpl<B extends AbstractBusinessObject, V extends ValueObject> implements BusinessFacade<B, V> {

        @Transactional(propagation= Propagation.REQUIRES_NEW)
        public void create(B b) {
                getDao().create(b);
        }

        @Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
        public List<B> findAll() {
                return getDao().findAll();
        }

        @Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
        public B findById(Long id) {
                return getDao().findByPrimaryKey(id);
        }

        @Transactional(propagation=Propagation.REQUIRES_NEW)
        public void remove(B b) {
                getDao().delete(b);
        }

        @Transactional(propagation=Propagation.REQUIRES_NEW)
        public void remove(Long id) {
                // TODO ver si agrego u método al dao
                B b = findById(id);
                remove(b);
        }

        @Transactional(propagation=Propagation.REQUIRES_NEW)
        public void removeAll() {
                // TODO ver si agrego u método al dao
                List<B> rows = findAll();
                for (B b : rows) remove(b);
        }

        @Transactional(propagation=Propagation.REQUIRES_NEW)
        public void update(B b) {
                getDao().update(b);
        }

        protected abstract GenericDao<B,V> getDao();

}